#include "ModelPosture.h"
#include "ModelPostureBone.h"
#include <tinyxml.h>

ModelPosture::ModelPosture() {}
ModelPosture::ModelPosture(const ModelPosture&) {}
ModelPosture::~ModelPosture() {}

void ModelPosture::Initialize(TiXmlElement* ModelPostureElement)
{
	const char* strId = ModelPostureElement->Attribute("id");
	if(strId != NULL) mId = ::atoi(strId);
	else mId = -1;

	const char* strName = ModelPostureElement->Attribute("name");
	if(strName != NULL) mName = std::string(strName);
	else mName = "";

	
	const char* strDuration = ModelPostureElement->Attribute("duration");
	if(strDuration != NULL) mDuration = ::atoi(strDuration);
	else mDuration = 0;

 	TiXmlElement* positionBoneElement = ModelPostureElement->FirstChildElement( "PostureBone" );
	for( positionBoneElement; positionBoneElement; positionBoneElement=positionBoneElement->NextSiblingElement("PostureBone") )
	{
		ModelPostureBone* modelPostureBone = new ModelPostureBone();
		modelPostureBone->Initialize(positionBoneElement);
		mModelPostureBones.push_back(modelPostureBone);
		mBoneNameToPostureBones[modelPostureBone->GetBoneName()] = modelPostureBone;
	}
}

ModelPostureBone* ModelPosture::GetModelPostureBoneByName(std::string pBoneName)
{
	std::map<std::string,ModelPostureBone*>::iterator postureBoneIt = mBoneNameToPostureBones.find(pBoneName);
	if ( postureBoneIt == mBoneNameToPostureBones.end() ) {
		return NULL;
	} else {
		return (*postureBoneIt).second;
	}
}

std::string ModelPosture::GetName()
{
	return mName;
}

int ModelPosture::GetDuration()
{
	return mDuration;
}